package com.allwees.bs.module.scm.service.impl;

import com.allwees.bs.core.modelbase.function.FunctionValidatable;
import com.allwees.bs.core.modelbase.transform.ObjectTransformFactory;
import com.allwees.bs.core.modelbase.transform.TransformFactoryStrategy;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.scm.dao.ScmAllocateOrderSkuDao;
import com.allwees.bs.module.scm.entity.ScmAllocateOrderSku;
import com.allwees.bs.module.scm.req.AllocateOrderSkuJoinPickReq;
import com.allwees.bs.module.scm.req.query.ScmAllocateOrderSkuQueryReq;
import com.allwees.bs.module.scm.service.ScmAllocateOrderSkuService;
import com.allwees.bs.module.scm.service.ScmPickOrderSkuService;
import com.allwees.core.common.constant.EStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * scm 分配订单
 *
 * @author mouhn
 * @since 2019-12-31 11:45
 */
@Service
public class ScmAllocateOrderSkuServiceImpl extends BoServiceImpl<ScmAllocateOrderSkuDao, ScmAllocateOrderSku> implements ScmAllocateOrderSkuService {

    @Autowired
    private ScmPickOrderSkuService pickOrderSkuService;

    @Override
    public Integer create(List<OmsOrderItem> omsOrderItems) {

        ObjectTransformFactory<OmsOrderItem, ScmAllocateOrderSku> factory = TransformFactoryStrategy.strategy(OmsOrderItem.class, ScmAllocateOrderSku.class);

        int successSum = 0;
        for (OmsOrderItem omsOrderItem : omsOrderItems) {

            ScmAllocateOrderSku allocateOrderSku = baseMapper.findByOrderItemUuid(omsOrderItem.getUuid());
            if (allocateOrderSku != null) {
                continue;
            }

            if (create(factory.transform(omsOrderItem))) {
                successSum++;
            }
        }
        return successSum;
    }

    @Override
    @Transactional
    public Integer joinPick(AllocateOrderSkuJoinPickReq req) {
        int count = _updateStatus(req.getAllocateOrderSkuUuids(), EStatus.SCM_JOINED_PICK, o -> {
            o.assertStatus(EStatus.NORMAL);
        });

        pickOrderSkuService.create(findByUuids(req.getAllocateOrderSkuUuids()));
        return count;
    }

    @Override
    @Transactional
    public boolean backFromPick(String uuid) {

        ScmAllocateOrderSku allocateOrderSku = checkByUuid(uuid);
        allocateOrderSku.assertStatus(EStatus.USED);
        allocateOrderSku.setEStatus(EStatus.NORMAL);

        return updateByUuid(allocateOrderSku);
    }

    @Override
    public void export(ScmAllocateOrderSkuQueryReq queryReq) {
    }

    @Override
    public void refundReason(String uuid) {
    }

    @Override
	public Integer _updateStatus(Set<String> allocateOrderSkuUuids, EStatus status, FunctionValidatable<ScmAllocateOrderSku> validatable) {
        int successSum = 0;
        for (String uuid : allocateOrderSkuUuids) {
            if (!_updateStatus(uuid, status, validatable)) {
                continue;
            }
            successSum++;
        }
        return successSum;
    }

    private boolean _updateStatus(String uuid, EStatus status, FunctionValidatable<ScmAllocateOrderSku> validatable) {
        ScmAllocateOrderSku allocateOrderSku = findByUuid(uuid);
        if (validatable != null) {
            validatable.validate(allocateOrderSku);
        }

        ScmAllocateOrderSku update = new ScmAllocateOrderSku();
        update.setEStatus(status);
        update.setUuid(uuid);
        return updateByUuid(update);
    }
}
